<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>compareVersion</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const compareVersion = (source, target) => {
            const a1 = source.split('.');
            const a2 = target.split('.');
            const len = Math.max(a1.length, a2.length);

            for (let i = 0; i < len; i++) {
                const v1 = +a1[i] || 0;
                const v2 = +a2[i] || 0;
                if (v1 > v2) {
                    return 1;
                }
                if (v1 < v2) {
                    return -1;
                }
            }
            return 0;
        };
        console.log(
            compareVersion('1.0.3', '1.0.5'), // 返回 -1
            compareVersion('1.0.7', '1.0.5'), // 返回 1
            compareVersion('1.1.3', '1.0.5'), // 返回 1
        );
    </script>
</body>

</html>